using System;
using System.Data;
using System.Windows.Forms;
using System.IO;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the StockExchanges table. It enables loading from a file,
    ///saving to a file and more
    /// </summary>
    public partial class StockExchangesEditor : UserControl
    {
        public DSIPAMS.DTStockExchangesDataTable stockExchanges;
        string[] SEHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="StockExchangesEditor"/> class.
        /// </summary>
        public StockExchangesEditor()
        {
            InitializeComponent();
            InitializeComponentExtneded();
        }

        /// <summary>
        /// Initializes the component extneded.
        /// </summary>
        private void InitializeComponentExtneded()
        {
           stockExchanges = new DSIPAMS.DTStockExchangesDataTable();
           dgvStockExchanges.DataSource = stockExchanges;
        }

        /// <summary>
        /// Handles the DataSourceChanged event of the dgvStockExchanges control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void dgvStockExchanges_DataSourceChanged(object sender, EventArgs e)
        {
            dgvStockExchanges.Refresh();
        }

        /// <summary>
        /// Loads the data from a file .
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns>a DataTable with the file data</returns>
        public DataTable LoadData(string dataFilePath)
        {
            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                SEHeaders = sr.ReadLine().Split(',');
                ;
                while (true)
                {
                    string stockExchange = sr.ReadLine();
                    if (stockExchange == string.Empty || stockExchange == null || stockExchange == @",,") break;
                    string[] stockExchangeInfo = stockExchange.Split(',');
                    if (stockExchangeInfo.Length != 3) continue;
                    stockExchanges.AddDTStockExchangesRow(stockExchangeInfo[0], stockExchangeInfo[1],
                                                          stockExchangeInfo[2]);
                }
                sr.Close();
                Text = dataFilePath;

                stockExchanges.AcceptChanges();
                dgvStockExchanges.DataSource = stockExchanges;

                for (int index = 0; index < SEHeaders.Length; index++)
                {
                    dgvStockExchanges.Columns[index].HeaderText = SEHeaders[index];
                }
                foreach (DataGridViewColumn column in dgvStockExchanges.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }


            return stockExchanges;
        }

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = SEHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTStockExchangesRow stockExchange in stockExchanges)
            {
                fileContent += string.Format("{0},{1},{2}" + Environment.NewLine, stockExchange[0], stockExchange[1], stockExchange[2]);
            }
            sw.Write(fileContent);
            sw.Close();

        }
    }
}
